/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package camada.bancodedados.daoimplemente.cfop;

import camada.bancodedados.conexao.Conexao;
import camada.negocio.dao.cfop.CFOPDAO;
import eloma.classe.fiscais.CFOP;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Icaro
 */
public class CFOPDAOImpl implements CFOPDAO {

    private static Connection con;
    PreparedStatement stmt = null;
    ResultSet rs = null;

    @Override
    public void inserir(CFOP c) throws Exception {

        try {

            con = Conexao.getCon();
            con.setAutoCommit(false);

            final String SQL_SCRIPT = "insert into CFOP (cfop, descricao, aplicacoes) values (?,?,?)";

            stmt = con.prepareStatement(SQL_SCRIPT);
            stmt.setInt(1, c.getCFOP());
            stmt.setString(2, c.getDescricao());
            stmt.setString(3, c.getAplicacoes());

            stmt.executeUpdate();

            con.commit();

        } catch (SQLException e) {
            con.rollback();
        } catch (Exception e) {
            con.rollback();
        }
    }


    @Override
    public void atualizar(CFOP c) throws Exception {

        try {

            final String SQL_SCRIPT = "update CFOP set CFOP = ?,descricao = ?, aplicacoes = ? where CFOP = ?";

            con = Conexao.getCon();
            con.setAutoCommit(false);

            stmt = con.prepareStatement(SQL_SCRIPT);
            stmt.setInt(1, c.getCFOP());
            stmt.setString(2, c.getDescricao());
            stmt.setString(3, c.getAplicacoes());
            stmt.setInt(4, c.getCFOP());

            stmt.executeUpdate();

            con.commit();

        } catch (SQLException e) {
            con.rollback();
        } catch (Exception e) {
            con.rollback();
        }
    }

    @Override
    public void remover(CFOP c) throws Exception {

        try {

            final String SQL_SCRIPT = "delete from CFOP where CFOP = ?";

            con = Conexao.getCon();
            con.setAutoCommit(false);

            stmt = con.prepareStatement(SQL_SCRIPT);
            stmt.setInt(1, c.getCFOP());
            stmt.executeUpdate();

            con.commit();

        } catch (SQLException e) {
            con.rollback();
        } catch (Exception e) {
            con.rollback();
        }

    }

    @Override
    public List<CFOP> buscaTodos() throws Exception {

        try {

            String SQL_SCRIPT = "Select * from CFOP order by CFOP";

            con = Conexao.getCon();
            con.setAutoCommit(false);

            stmt = con.prepareStatement(SQL_SCRIPT);
            rs = stmt.executeQuery();

            con.commit();

        } catch (SQLException sqle) {
            con.rollback();
        } catch (Exception e) {
            con.rollback();
        }

        return carregarMultiplosResultados(rs);

    }

    public List<CFOP> carregarMultiplosResultados(ResultSet rs) throws SQLException {
        List<CFOP> resultList = new ArrayList<CFOP>();
        while (rs.next()) {
            CFOP dto = new CFOP();
            carregarVO(dto, rs);
            resultList.add(dto);
        }
        return resultList;
    }

    private CFOP carregarResultadoSimples(ResultSet rs) throws SQLException {
        if (rs.next()) {
            CFOP dto = new CFOP();
            carregarVO(dto, rs);
            return dto;
        } else {
            return null;
        }
    }

    private void carregarVO(CFOP dto, ResultSet rs) throws SQLException {
        dto.setCFOP(rs.getInt("CFOP"));
        dto.setDescricao(rs.getString("descricao"));
        dto.setAplicacoes(rs.getString("aplicacoes"));
    }

}
